﻿<!-- 综合监控页面 -->

<div id="allmap" class ="baidu-maps" style="width: 100%;height: 100%"></div>
<!-- 报警设置表单开始 -->
<form  class="layui-form layui-form-pane " action="" id="set_param_form" lay-filter="set_param_form" style="padding: 10px;display: none;">
  <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
    <legend style="font-size: 15px">心率报警</legend>
  </fieldset>
   <div id="heart_rate_slider" class="heart_rate_slider"></div>
   <br>
  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">心率低于</label>
      <div class="layui-input-block">
        <input id="heart_low" type="text" name="heart_low"  placeholder="40"  autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">心率高于</label>
      <div class="layui-input-block">
        <input id="heart_hight" type="text" name="heart_hight" lay-verify="required" autocomplete="off" placeholder="150" class="layui-input">
      </div>
    </div>
  </div>

  <div class="layui-form-item">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
      <legend style="font-size: 15px">血压高压报警</legend>
    </fieldset>
     <div id="h_pres_slider" class="heart_rate_slider"></div>
     <br>
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">高压低于</label>
      <div class="layui-input-block">
        <input id="h_pres_low" type="text" name="h_pres_low"  placeholder=""  autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">高压高于</label>
      <div class="layui-input-block">
        <input id="h_pres_hight" type="text" name="h_pres_hight" lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
      </div>
    </div>
  </div>

  <div class="layui-form-item">
      <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
        <legend style="font-size: 15px">血压低压报警</legend>
      </fieldset>
       <div id="l_pres_slider" class="heart_rate_slider"></div>
       <br>
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">低压低于</label>
        <div class="layui-input-block">
          <input id="l_pres_low" type="text" name="l_pres_low"  placeholder=""  autocomplete="off" class="layui-input">
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">低压高于</label>
        <div class="layui-input-block">
          <input id="l_pres_hight" type="text" name="l_pres_hight" lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
        </div>
      </div>
  </div>

  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">温度报警</label>
      <div class="layui-input-inline">
        <input type="text" name="tem_alarm"  placeholder="C°"  autocomplete="off" class="layui-input">
      </div>
      <div class="layui-form-mid layui-word-aux">C°</div>
    </div>
  </div>

  <div class="layui-form-item" style="display: none;">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">停留报警</label>
      <div class="layui-input-block">
        <input type="text" name="stop_rate"  placeholder="分钟"  autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-inline" >
      <label class="layui-form-label layui-bg-green">设备编号</label>
      <div class="layui-input-block">
        <input id="alarm_set_device" type="text" name="alarm_set_device"  placeholder="分钟"  autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>

  <div class="layui-form-item">
    <button class="layui-btn" lay-submit="" lay-filter="data_handle_btn" id="set_param_submit" style="display: none;"></button>
  </div>
</form>

<!-- 报警设置表单结束 -->


<!-- 命令下发弹窗开始 -->
<!-- 命令下发表格的执行按钮 -->
<script type="text/html" id="command_table_bar">
    <a class="layui-btn layui-btn-xs" lay-event="excue_command">执行</a>
</script>
<form  class="layui-form layui-form-pane " action="" id="command_form" lay-filter="command_form" style="padding: 10px;display: none;">
  <table class="layui-table" id="command_down_table" lay-size="lg" lay-filter="command_table_filter">
  </table>
</form>
<!-- 命令下发弹窗结束 -->

<!-- 硬件参数设置开始 -->
<form  class="layui-form layui-form-pane " action="" id="param_set_form" lay-filter="param_set_form" style="padding: 10px;display: none;">
  <input type="" name="" id="set_device_id" style="display: none;">
  <div class="layui-col-lg6">
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">回传间隔</label>
        <div class="layui-input-inline">
          <input id="send_rate" type="text" name="send_rate"  placeholder="秒" value="30"  autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">秒</div>
        <a class="layui-btn" onclick="device_set('send_rate',$('#set_device_id').val(),$('#send_rate').val())"><i class="layui-icon layui-icon-set" style="font-size: 12px"> 提交</i></a>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">健康监测频率</label>
        <div class="layui-input-inline">
          <input id="health_rate" type="text" name="health_rate"  placeholder="秒" value="300"  autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">秒</div>
        <a class="layui-btn" onclick="device_set('health_rate',$('#set_device_id').val(),$('#health_rate').val())"><i class="layui-icon layui-icon-set" style="font-size: 12px"> 提交</i></a>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">中心号码</label>
        <div class="layui-input-inline">
          <input id="center_num_set" type="text" name="center_num_set"  placeholder="主要电话号码"   autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">—</div>
        <a class="layui-btn" onclick="device_set('center_num_set',$('#set_device_id').val(),$('#center_num_set').val())"><i class="layui-icon layui-icon-set" style="font-size: 12px"> 提交</i></a>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">SOS号码</label>
        <div class="layui-input-inline">
          <input id="sos1" type="text" name="sos1"  placeholder="SOS1,SOS2,SOS3" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">—</div>
        <a class="layui-btn" onclick="device_set('sos1',$('#set_device_id').val(),$('#sos1').val())"><i class="layui-icon layui-icon-set" style="font-size: 12px"> 提交</i></a>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">工作模式</label>
        <div class="layui-input-inline">
          <select id="work_model" name="work_model"  >
            <option value="2">省电</option>
            <option value="1" selected="">普通</option>
            <option value="3">紧急</option>
          </select>
        </div>
        <div class="layui-form-mid layui-word-aux">—</div>
        <a class="layui-btn" onclick="device_set('work_model',$('#set_device_id').val(),$('#work_model').val())"><i class="layui-icon layui-icon-set" style="font-size: 12px"> 提交</i></a>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">计步时间</label>
        <div class="layui-input-inline">
          <input id="walk_time_set" type="text" name="walk_time_set"  placeholder="采集运动步数时间段" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">—</div>
        <a class="layui-btn" onclick="device_set('walk_time_set',$('#set_device_id').val(),$('#walk_time_set').val())"><i class="layui-icon layui-icon-set" style="font-size: 12px"> 提交</i></a>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">禁用时间</label>
        <div class="layui-input-inline">
          <input id="silence_time_set" type="text" name="silence_time_set"  placeholder="禁止接收来电时间段" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">—</div>
        <a class="layui-btn" onclick="device_set('silence_time_set',$('#set_device_id').val(),$('#silence_time_set').val())"><i class="layui-icon layui-icon-set" style="font-size: 12px"> 提交</i></a>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">翻滚检测</label>
        <div class="layui-input-inline">
          <input id="sleep_time" type="text" name="sleep_time"  placeholder="检测设备翻转时间段" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">—</div>
        <a class="layui-btn" onclick="device_set('sleep_time',$('#set_device_id').val(),$('#sleep_time').val())"><i class="layui-icon layui-icon-set" style="font-size: 12px"> 提交</i></a>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">闹钟设置</label>
        <div class="layui-input-inline">
          <input id="time_remind" type="text" name="time_remind"  placeholder="闹钟设置" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">—</div>
        <a class="layui-btn" onclick="device_set('time_remind',$('#set_device_id').val(),$('#time_remind').val())"><i class="layui-icon layui-icon-set" style="font-size: 12px"> 提交</i></a>
      </div>
    </div>

  </div>
  <!-- 弹窗另一半 -->
  <div class="layui-col-lg6">
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green" style="width: 120px">SOS短信开关</label>
        <div class="layui-input-inline">
          <input type="checkbox" checked="" name="sosmsg_to_phone" lay-skin="switch" lay-filter="dev_func_switch" lay-text="ON|OFF">
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green" style="width: 120px">低电短信报警</label>
        <div class="layui-input-inline">
          <input type="checkbox" checked="" name="low_power_msg" lay-skin="switch" lay-filter="dev_func_switch" lay-text="ON|OFF">
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green" style="width: 120px">设备拆除报警</label>
        <div class="layui-input-inline">
          <input type="checkbox" checked="" name="dev_remove_alarm" lay-skin="switch" lay-filter="dev_func_switch" lay-text="ON|OFF">
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green" style="width: 120px">设备拆除短信</label>
        <div class="layui-input-inline">
          <input type="checkbox" checked="" name="dev_remove_msg" lay-skin="switch" lay-filter="dev_func_switch" lay-text="ON|OFF">
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green" style="width: 120px">计步功能开关</label>
        <div class="layui-input-inline">
          <input type="checkbox" checked="" name="step_cunt_btn" lay-skin="switch" lay-filter="dev_func_switch" lay-text="ON|OFF">
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green" style="width: 120px">摔倒报警开关</label>
        <div class="layui-input-inline">
          <input type="checkbox" checked="" name="fall_down" lay-skin="switch" lay-filter="dev_func_switch" lay-text="ON|OFF">
        </div>
      </div>
    </div>

  </div>
</form>

<!-- 硬件参数设置结束 -->

<!-- 手机用户绑定设备开始 -->
<form  class="layui-form layui-form-pane " action="" id="bind_dev_phoneuser" lay-filter="bind_dev_phoneuser" style="padding: 10px;display: none;">
  <div class="layui-form-item" style="color: white;font-size: 16px">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">设备ID</label>
      <div class="layui-input-inline">
        <input type="text" name="device_id" lay-verify="required" autocomplete="off" placeholder="设备ID" class="layui-input">
      </div>
    </div>
  </div>
   <div class="layui-form-item" style="color: white;font-size: 16px">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">授权码</label>
      <div class="layui-input-inline">
        <input type="text" name="auth_id" lay-verify="required"  placeholder="授权码"  autocomplete="off" class="layui-input">
      </div>
  </div>
  <button id="submit_btn" lay-submit lay-filter="bind_dev_phoneuser" style="display: none;">提交</button>
</form>
<!-- 手机用户绑定设备结束 -->

<script type="text/javascript">

// ------手机注册用户绑定设备功能开始--------//
console.log(character['bind_dev'],user_permi)

var show_bind_dev_form=function(){
  form.render()
  layer.open({
    type: 1
    ,title:'绑定设备'
    ,area:['400px','240px']
    ,offset:'60px'  
    ,id: 'register_data' 
    ,content:$('#bind_dev_phoneuser')
    ,btn: ['提交','关闭']
    ,shade: 0 //不显示遮罩
    ,btn1: function(){
      $('#submit_btn').click()
      
    }
    ,btn2: function(){
      layer.closeAll();
    }
  }); 
}
  
form.on('submit(bind_dev_phoneuser)',function(obj){
  console.log(obj.field)
  layer.closeAll()
  var param_data=obj.field
  $.ajax({
    type: 'POST',
    data:param_data,
    url:'/dynamic/?what=phone_user_bind_dev',
    async:true,
    dataType: 'json',
    success: function(data) {
      // console.log(JSON.stringify(data))
      if(data.code==1000){
        layer.msg('绑定设备成功')
        device_list_table(data.data)
      }else{
        alert(data.error)
      }
    },
  });
  return false
})

if(user_permi=='phone_user' && character['bind_dev']==null){
  show_bind_dev_form()
}
// ------手机注册用户绑定设备功能结束--------//

// -------高德地图API调用相关功能开始--------//

function onApiLoaded(){
  var map = new AMap.Map('allmap', {
      center: [117.000923, 36.675807],
      zoom: 6
  });
  map.plugin(["AMap.ToolBar"], function() {
      map.addControl(new AMap.ToolBar());
  });
}
var url = 'https://webapi.amap.com/maps?v=2.0&key=a66c5cf868a7a3c621e4bcb4aba57a4d&callback=onApiLoaded';
var jsapi = document.createElement('script');
jsapi.charset = 'utf-8';
jsapi.src = url;
document.head.appendChild(jsapi);

// 设备数据刷新函数
var get_new_device=function(ajax_url='get_new_device'){

  $.ajax({
        type: 'GET',
        url:"/dynamic/?what="+ajax_url,
        async:true,
        dataType: 'json',
        success: function(data) {
          console.log('refresh_device')
          device=data;
          for(var i=0;i<data.length;i++){
            if(data[i]['device_id'] in checkDiv){
              data[i]["LAY_CHECKED"]=true
            }
          }
          device_list_table(data)
        },
      });

    setTimeout(function(){
      get_new_device(ajax_url);
    },param_refresh_time_control)
}


setTimeout(function(){
  get_new_device();
},param_refresh_time_control)



// 点击设备列表的设备，进行地图打点展示
var on_check=function(row){
  var fix_data=wgs2bd(row.lat,row.lng)
  var lat=fix_data[0]
  var lng=fix_data[1]
  var point=new BMap.Point(lng,lat)
  gps_location(point,row.device_id)
  //point_all[row.gps_imei_id]=point
  setTimeout(function(){
   addmaker(row,point);
 },100)
}


//地图打点功能函数
var addmaker=function(gps,point){
  var myIcon
  myIcon = new BMap.Icon("/static/device_icon/track_button.png", new BMap.Size(72,144),{anchor : new BMap.Size(27, 13),imageSize:new BMap.Size(22, 30)});
  map.centerAndZoom(point, 15);
  var dev_name=gps.user_name
  if(gps.user_detail!='0'){
    dev_name=gps.user_detail.name
  }
  mylabel = new BMap.Label(dev_name,{offset:new BMap.Size(-10,-20)});//设置标签
  mylabel.setStyle({ color : "bule", fontSize : "15px" })
  marker = new BMap.Marker(point,{icon:myIcon});
  marker.dev_datail=gps
  marker.addEventListener('click', function (e) {
    open_infowindow(e.target.dev_datail,e.point)
  })
  map.addOverlay(marker);//加载定位点
  marker.setTitle(gps.device_id);//设置标题
  marker.setLabel(mylabel);
  open_infowindow(gps,point)
};


//删除地图定位点
var remove=function(imei){
    fugaiwu = map.getOverlays();
    for(var i=0,n=fugaiwu.length;i<n;i++){
      if (fugaiwu[i] instanceof BMap.Marker) {
        if(fugaiwu[i].toString()=="[object Marker]"){
          if (fugaiwu[i].getTitle() == imei) {
                      fugaiwu[i].closeInfoWindow();
          map.removeOverlay(fugaiwu[i]); 
          return 'removemarker';
      };
    };
  };
};
};

// 地址逆解析，从经纬度变成文字地址，百度接口
var postion_all=new Array;
var gps_location=function(pt,gps_id){
      var location
      geoc.getLocation(pt, function(rs){
      var addComp = rs.address
      if(rs.surroundingPois.length!=0){
        // console.log(rs)
         location=addComp+','+rs.surroundingPois[0]['title']
         postion_all[gps_id]=location;
         for(i=0;i<device.length;i++){
          if(device[i]['device_id']==gps_id){
            device[i]['position']=location
          };
         };      
      }else{
         location=addComp
         postion_all[gps_id]=location;
         for(i=0;i<device.length;i++){
          if(device[i]['device_id']==gps_id){
            device[i]['position']=location
          };
         };   
      }
    });
  };

// 解析wifi和lbs地址，后台只匹配在数据库里面的存储好的lbs和wifi地址，没有的，需要用户单独点击获取地址，后台进行解析处理
var get_wifi_lbs_addr=function(device_id,track_type){

  var dev_id={}
  dev_id['device_id']=device_id
  dev_id['track_type']=track_type
  $.ajax({
      type: 'POST',
      data:dev_id,
      url:"/dynamic/?what=get_wifi_lbs_addr",
      async:true,
      dataType: 'json',
      success: function(address_data) {
        console.log(address_data)
        address_data=address_data[0]
        if(address_data['lng']==0){
          layer.msg('无法匹配该'+track_type+'地址')
        }else{
          for(var i=0;i<device.length;i++){
            if(device_id==device[i]['device_id']){
              device[i]['lng']=address_data['lng']
              device[i]['lat']=address_data['lat']
              remove(device_id)
              on_check(device[i])
            }
          }
        }
      },
  });
}
//var point_all=new Array;
//定位点信息窗展示功能
var open_infowindow=function(gps,point){
  var bds_num=gps.dynamic.bds_num,param1,param2,param3,param4,blood_pres,gps_num=gps.dynamic.gps_num,rssi=gps.rssi;
    var dev_kind='0'
    if(gps.dynamic.receive_port!='0'){
      dev_kind=gps.dynamic.receive_port[1]
    }
    var dev_loca=postion_all[gps.device_id]
    // console.log(dev_loca)
    if(dev_loca=='' && gps.dynamic.track_type!=='0'){
      wifi_lbs={'wifi':gps.dynamic.wifi,'lbs':gps.dynamic.lbs}
      dev_loca='<button class="layui-btn layui-btn-xs" onclick="get_wifi_lbs_addr('+gps.device_id+',\''+gps.dynamic.track_type+'\')">点击解析'+gps.dynamic.track_type+'地址</button>'
    }

    // var status_list=['acc_off', 'ante_ok', 'undefence', 'normal','online','sleep']
    status_dict={'undefence':'未设防', 'normal':'正常','online':'在线','sleep':'休眠','sos_alarm':'SOS报警'}
    if(bds_num==null){
      bds_num='0'
    }
    if(gps_num==null){
      gps_num='0'
    }
    // var stop_time=(eval('('+gps.stop_time+')')['stop_time']/60).toFixed(0)
    var stop_time='0'
    var g_status;
    var status=''
    if(gps.on_off_line=='online'){
      g_status='1'
    }else{
      g_status='0'
    }
    // 状态信息
    for(var i=0;i<gps.status.length;i++){
      if(gps.status[i] in status_dict){
        status+=status_dict[gps.status[i]]+','
      }else{
        status+=gps.status[i]+','
      }
    }
    
    blood_pres=''
    if(gps.dynamic.blood_pres!='0'){
      blood_pres=eval('('+gps.dynamic.blood_pres+')')
      blood_pres='高：'+blood_pres['h_pres']+','+'低：'+blood_pres['l_pres']
    }
    heart_rate=''
    if(gps.dynamic.heart_rate!='0'){
      heart_rate=gps.dynamic.heart_rate
    }

    // 绑定的用户信息
    var user_detail=gps.user_detail;
    // if(user_detail!='0' | user_detail!=0){
    //   user_detail=gps.user_detail.current
    // }
    var dev_user_name=gps.user_name;
    var user_sex='',user_department='',user_character='',user_age='',user_picture='';
    if(user_detail!='0' | user_detail!=0){
      user_sex=user_detail.sex
      var person_detail=eval('('+user_detail.person_detail+')')
      user_department=person_detail.department
      user_character=person_detail.character
      user_age=person_detail.person_age
      user_picture=person_detail.picture
      dev_user_name=user_detail.name
    }
    if(user_picture==='' | user_picture==null){
      user_picture='/static/img/person_default.png'
    }
    var param1='性别：'+user_sex
    var param2='年龄：'+user_age
    var param3='部门：'+user_department
    var param4='职位：'+user_character

        //信息窗口参数
      var opts = {
        width : 470,     // 信息窗口宽度
        height: 450,     // 信息窗口高度
        };
      var info_window_baidu;
      info_window_baidu=
     "<div>"+
      "<table class='layui-table' lay-size='sm' style='font-size:13px;'>"+
      "<tbody>"+
      "<tr>"+
      "<td  style = 'font-size:13px;width:250px'>"+
      "<table class='layui-table' lay-size='sm' style='font-size:13px;width:300px'>"+
      "<tr bgcolor='#84C1FF'>"+
      "<td bgcolor='#009688'><font color='white'>"+"名称 : "+dev_user_name+"</font></td>"+
      "</tr>"+
      "<tr>"+
      "<td >"+param1+"</td>"+
      "</tr>"+
      "<tr>"+
      "<td >"+param2+"</td>"+
      "</tr>"+
      "<tr>"+
      "<td >"+param3+"</td>"+
      "</tr>"+
      "<tr>"+
      "<td >"+param4+"</td>"+
      "</tr>"+
      "</table>"+ 
      "</td>"+
      "<td ><img src='"+user_picture+"' style='width:155px;height:auto'></td>"+
      "</tr>"+
      "<table class='layui-table' lay-size='sm' style='font-size:13px;'>"+
      "<tbody>"+
      "<tr >"+
      "<td colspan=2 bgcolor='#009688' ><font color='white'>"+"设备ID : "+gps.device_id+"</font></td>"+
      "</tr>"+
      "<tr>"+
      "<td colspan=2>"+
      "<div class='layui-row'>"+
      "<div class='layui-col-lg7'>"+
      " 当前状态："+status+
      "</div>"+
      "<div class='layui-col-lg5'>"+
      "<i class='fa fa-battery-full pull-right pull-right' style = 'font-size:13px; padding-right: 9px;color: #009688;' > "+gps.battery+"</i>"+
      "<span class='fa fa-broadcast-tower pull-right'  style = 'font-size:12px; padding-right: 9px;color: #009688;'>"+rssi+"</span>"+
      "<span class='fa fa-satellite-dish pull-right'  style = 'font-size:12px; padding-right: 9px;color: #009688;'>"+gps_num+"</span>"+
      "<span class='fa fa-satellite pull-right'  style = 'font-size:12px; padding-right: 9px;color: #009688;'>"+bds_num+"</span>"+
      "</div>"+
      "</div>"+
      "</td>"+
      "<tr  id='heart_blood'>"+"<td>"+"心律数值："+heart_rate+"次/分</td>"+"<td>"+"血压数值 : "+blood_pres+"</td>"+
      "</tr>"+
       "<tr  id='show_add'>"+"<td>"+"温度："+stop_time+"C°</td>"+"<td>"+"运动步数 : "+gps.dynamic.step_count+"步</td>"+
      "</tr>"+
      "<tr>"+
      "<td>"+" 通讯时间："+gps.serv_receive+"</td>"+"<td colspan=2>"+" 定位类型："+gps.dynamic.track_type+"</td>"+
      "</tr>"+
      "<tr>"+
      "<td colspan=2>"+"位置:"+dev_loca+"</td>"+
      "</tr>"+
      "</tbody>"+
      "</table>"+ 
        "</tbody>"+
    "</table>"+ 

      // "<div class='btn-group' style = 'font-size:15px; padding-right: 9px;'>"+
      // "<button class='btn btn-info' onclick='detail("+gps.device_id+")' >"+"资料详情"+"</button>"+
      // "</div>"+

      "<div class='layui-btn-group layui-col-lg-3' style = 'font-size:15px; padding-right: 25px;'>"+
      "<button class='layui-btn layui-btn-sm' onclick='turn_to_trail("+gps.device_id+")'>"+"轨迹回放"+"</button>"+
      "</div>"+

      "<div class='layui-btn-group layui-col-lg-3' style = 'font-size:15px; padding-right: 25px;'>"+
      "<button class='layui-btn layui-btn-sm ' onclick='set_param("+gps.device_id+","+g_status+")'>"+"报警参数"+"</button>"+
      "</div>"+

      "<div class='layui-btn-group layui-col-lg-3' style = 'font-size:15px; padding-right: 25px;'>"+
      "<button class='layui-btn layui-btn-sm' onclick='command_modal("+gps.device_id+","+g_status+")'>"+"命令下发"+"</button>"+
      "</div>"+

      "<div class='layui-btn-group layui-col-lg-3' style = 'font-size:15px;padding-right: 25px;'>"+
      "<button class='layui-btn layui-btn-sm' onclick='device_set_modal("+gps.device_id+","+g_status+")'>"+"参数设置"+"</button>"+
      "</div>"+

    "</div>"
    ;
      var gpsinfoWindow = new BMap.InfoWindow(info_window_baidu, opts);
      map.openInfoWindow(gpsinfoWindow,point);
};


// 跳转到轨迹回放

var turn_to_trail=function(device_id){
  trail_device=device_id
  turn_to_page('trail')
}

var checkDiv=new Array();
// 监听设备列表点击
table.on('row(dev_list_show)', function(obj){
  click_dev_id(obj)
});

var click_dev_id=function(obj){//选中行同步选中复选按钮
  var tr = obj.tr;
  var div = tr.find("div.layui-unselect");
  //checkDiv为定义的属性用来缓存当前点击的行是否选中状态，未选择的选中，选中的取消选中，并进行相应的地图打点处理
  // console.log(obj.data.device_id)
  if(!checkDiv[obj.data.device_id]) {
    checkDiv[obj.data.device_id] = obj;
    div.addClass("layui-form-checked");
    on_check(obj.data)
  } else {
    div.removeClass("layui-form-checked");
    delete checkDiv[obj.data.device_id];
    remove(obj.data.device_id)
  }
}

// -------百度地图API调用相关功能结束--------//

// 报警参数功能js部分开始
// 监听报警参数设置
var set_param=function(device_id,status){
    form.render();
    document.getElementById("set_param_form").reset();
    // 参数title字典
    var person_param={'alarm_set_device':device_id};
    // 获取设备报警参数设置信息，并赋值给弹窗
    for(var i=0;i<device.length;i++){
      if(device[i].device_id==device_id){
        var param_set=device[i]['param_set']
        if(param_set.length!=0){
          var heart_set=param_set['heart_set']
          var blood_set=param_set['blood_set']
          var stop_rate=param_set['stop_rate']
          var tem_alarm=param_set['tem_alarm']
          // console.log(blood_set,heart_set)
          if(heart_set!='0' && heart_set!=null){
            // heart_set=eval('('+heart_set+')')
            person_param['heart_low']=heart_set['heart_low']
            person_param['heart_hight']=heart_set['heart_hight']
            // 给滑块赋值
            heart_rate_slider.setValue(eval(heart_set['heart_low']),0)
            heart_rate_slider.setValue(eval(heart_set['heart_hight']),1)
          }
          if(blood_set!='0' && blood_set!=null){
            // blood_set=eval('('+blood_set+')')
            person_param['l_pres_low']=blood_set['l_pres_low']
            person_param['l_pres_hight']=blood_set['l_pres_hight']
            person_param['h_pres_low']=blood_set['h_pres_low']
            person_param['h_pres_hight']=blood_set['h_pres_hight']
            // 低压报警滑块赋值
            l_pres_slider.setValue(eval(blood_set['l_pres_low']),0)
            l_pres_slider.setValue(eval(blood_set['l_pres_hight']),1)
            // 高压报警滑块赋值
            h_pres_slider.setValue(eval(blood_set['h_pres_low']),0)
            h_pres_slider.setValue(eval(blood_set['h_pres_hight']),1)
          }
          if(stop_rate!='0' && stop_rate!=null){
            // stop_rate=eval('('+stop_rate+')')
            person_param['stop_rate']=stop_rate['stop_rate']
            // console.log(typeof(stop_rate))
          }
          if(tem_alarm!='0' && tem_alarm!=null){
            // stop_rate=eval('('+tem_alarm+')')
            person_param['tem_alarm']=tem_alarm['tem_alarm']
            // console.log(typeof(stop_rate))
          }
        }
      }
    }
    // 把设备之前的参数设置赋值给form
    form.val("set_param_form",person_param)
    
    layer.open({
        type: 1
        ,title:'报警参数设置: '+device_id
        ,area:['700px','600px']
        ,offset:'60px'  
        ,id: 'add_data' //防止重复弹出
        ,content:$('#set_param_form')
        ,btn: ['提交','关闭']
        ,shade: 0 //不显示遮罩
        ,btn1: function(){
            console.log('ok')
            $('#set_param_submit').click();
        }
        ,btn2: function(){
          layer.closeAll();
        }
      });
};
// 报警参数设置上传后台
form.on('submit(data_handle_btn)', function (data) {
    var row_data=data.field;
    var alarm_set_data={};
    alarm_set_data['heart_set']={'heart_hight':row_data['heart_hight'],'heart_low':row_data['heart_low']}
    alarm_set_data['blood_set']={'l_pres_low':row_data['l_pres_low'],'l_pres_hight':row_data['l_pres_hight'],
                                    'h_pres_low':row_data['h_pres_low'],'h_pres_hight':row_data['h_pres_hight']}
    alarm_set_data['stop_rate']={'stop_rate':row_data['stop_rate']}
    alarm_set_data['tem_alarm']={'tem_alarm':row_data['tem_alarm']}
    alarm_set_data['device_id']=row_data['alarm_set_device']
    alarm_set_data = JSON.stringify(alarm_set_data);
    console.log(alarm_set_data)
    $.ajax({
        type: 'POST',
        data:{alarm_set_data},
        url:"/dynamic/?what=alarm_set",
        async:true,
        dataType: 'json',
        success: function(data) {
          console.log(data)
          layer.closeAll();
          
        },
    });
    return false;
});

// 心率报警参数选择框渲染
//开启范围选择
var heart_rate_slider=slider.render({
  elem: '#heart_rate_slider'
  ,max:200
  ,value: [40,150] //初始值
  ,range: true //范围选择

  ,change: function(vals){
    $('#heart_low').val(vals[0]);
    $('#heart_hight').val(vals[1]);
  }
});

// 血压高压报警参数选择框渲染
//开启范围选择
var h_pres_slider=slider.render({
  elem: '#h_pres_slider'
  ,max:200
  ,value: [40,150] //初始值
  ,range: true //范围选择

  ,change: function(vals){
    $('#h_pres_low').val(vals[0]);
    $('#h_pres_hight').val(vals[1]);
  }
});

// 血压低压报警参数选择框渲染
//开启范围选择
var l_pres_slider=slider.render({
  elem: '#l_pres_slider'
  ,max:200
  ,value: [40,150] //初始值
  ,range: true //范围选择

  ,change: function(vals){
    $('#l_pres_low').val(vals[0]);
    $('#l_pres_hight').val(vals[1]);
  }
});
// 报警参数功能js部分结束

// 命令下发功能js部分开始
 // 监听命令下发弹窗
var command_dict={'call_now':'实时点名','factory_set':'恢复出厂设置','verson_detail':'查询版本信息','dev_reset':'远程重启','turn_off_dev':'远程关机',
                  'find_dev':'设备寻找','monitor':'监听','photograph':'远程拍照','health_detect':'健康检测'}

var command_modal=function(device_id,status){
  // 重新组装可以加载到表格的命令数据格式
    var command_detail=[]
    for(k in command_dict){
      var comm_value={};
      comm_value['command_style']=k
      comm_value['command_name']=command_dict[k]
      comm_value['device_id']=device_id
      command_detail.push(comm_value)

    }
   

    layer.open({
        type: 1
        ,title:'命令下发: '+device_id
        ,area:'300px'
        ,offset:'60px'  
        ,id: 'command_data' //防止重复弹出
        ,content:$('#command_form')
        ,btn: '关闭'
        ,shade: 0 //不显示遮罩
        ,success:function(layero,index){
            table.render({
            elem: '#command_down_table',
            data: command_detail,
            id:'command_down_table',
            skin:'nob',
            size:'lg',
            cols: [[
                {field: 'device_id', title: '设备ID',hide:true},
                {field: 'command_name',width:120, title: '名称',sort:true},
                {field: 'command_style', title: '命令格式',hide:true},
                {title: '操作',templet: '#command_table_bar', fixed: "right", align: "center"}
            ]],
            page: false
          });
        }
        ,btn1: function(){
          layer.closeAll();
        }
      });


};


//命令下发到后台进行处理
table.on('tool(command_table_filter)', function (obj) {
  var comm_result = obj.data;
  var param_data={'device_id':comm_result.device_id,'set_command':comm_result.command_style,'set_value':'1'}
  param_data=JSON.stringify(param_data)
  console.log(param_data)
  $.ajax({
    type: 'POST',
    data:{param_data},
    url:"/dynamic/?what="+"order_dowm",
    async:true,
    dataType: 'json',
    success: function(data) {
      if(data=='set_param,ok!'){
        layer.msg('操作成功')
      }else{
         layer.msg('操作失败')
      }
      console.log(data)
    },

  });
});
// 命令下发功能js部分结束

// 设备参数设置功能js部分开始
// var param_dict0={'send_rate':'回传频率','health_rate':'健康检测频率','center_num_set':'设置中心号码','phone_call':'拨打电话','sos1':'紧急呼叫号码1',
//         'sos2':'紧急呼叫号码2','sos3':'紧急呼叫号码3','ip_port':'服务器IP端口设置','time_language':'设备语言选择','sosmsg_to_phone':'SOS短信开关',
//         'low_power_msg':'低电量报警短信开关','dev_remove_alarm':'设备拆除报警开关','dev_remove_msg':'设备拆除短信开关','step_cunt_btn':'计步功能开关',
//         'work_model':'工作模式'}
// var param_dict1={'fall_down':'摔倒报警开关','walk_time_set':'计步时间设置','silence_time_set':'禁用时间设置','time_remind':'闹钟设置',
//                   'sleep_time':'设备翻滚检测时间设置','long_sit_alarm':'久坐提醒','take_medicine':'喝药提醒','drink_water':'喝水提醒'}
// var dialog_dict={'wav_msg':'下发语音','writing_msg':'下发文字','picture':'下发图片','video_talk':'视频通话'}
// var time_chose_list=['walk_time_set','silence_time_set','time_remind','sleep_time','long_sit_alarm','take_medicine','drink_water']
var device_set_modal=function(device_id,status){
  // 因为动态加载form，需要主动渲染form，解决select不显示的问题
  form.render()
  document.getElementById("param_set_form").reset();
  for(var i=0;i<device.length;i++){
    if(device[i].device_id==device_id){
      if(device[i].device_set!=='0'){
        var current_device_set=device[i].param_set
        // console.log(current_device_set)
        // 转换开关参数，使之能适应layui的开关显示
        var on_off_list=['sosmsg_to_phone','low_power_msg','dev_remove_alarm','dev_remove_msg','step_cunt_btn','fall_down']
        for(var k in current_device_set){
          if(on_off_list.indexOf(k)!==-1){
            var change_style={'0':false,'1':true}
            current_device_set[k]=change_style[current_device_set[k]]
          }
        }
        console.log(current_device_set)
        form.val('param_set_form',current_device_set)
      }
    }
  }
  $('#set_device_id').val(device_id)
  // 弹出弹窗
    parent.layer.open({
        type: 1
        ,title:'硬件参数设置: '+device_id
        ,area:'880px'
        ,offset:'60px'  
        ,id: 'command_data' //防止重复弹出
        ,content:$('#param_set_form')
        ,btn: '关闭'
        ,zIndex:999
        // 遮罩把弹窗盖住的问题的解决办法，把遮罩和弹窗放在同一层
        // ,success:function(layero){
        //   console.log(layero)
        //      var mask = $(".layui-layer-shade");
        //      mask.appendTo(layero.parent());
        //      //其中：layero是弹层的DOM对象
        // }
        ,shade: 0 //不显示遮罩
        ,btn1: function(){
          layer.closeAll();
        }
        
      });

// 监听设备功能开关设置并提交后台
form.on('switch(dev_func_switch)',function(obj){
  // console.log(obj.elem.name,obj.elem.checked,device_id)
  var set_command=obj.elem.name;
  var set_value=obj.elem.checked;
  if(set_value==true){
    set_value='1'
  }else{
    set_value='0'
  };
  device_set_ajax(device_id,set_command,set_value);
})
};

// 监听用户输入设备设置参数并提交后台
var device_set=function(set_command,device_id,set_value){
  device_set_ajax(device_id,set_command,set_value)
  return false
}
//命令下发到后台进行处理
var device_set_ajax=function(device_id,set_command,set_value){

  var param_data={'device_id':device_id,'set_command':set_command,'set_value':set_value}
  param_data=JSON.stringify(param_data)
  console.log(param_data)
  $.ajax({
    type: 'POST',
    data:{param_data},
    url:"/dynamic/?what="+'device_set',
    async:true,
    dataType: 'json',
    success: function(data) {
      if(data=='set_param,ok!'){
        layer.msg('操作成功')
      }else{
         layer.msg('操作失败')
      }
      // console.log(data)
    },
  });

}
//时间范围
laydate.render({
  elem: '#walk_time_set'
  ,type: 'time'
  ,range: true
});

laydate.render({
  elem: '#silence_time_set'
  ,type: 'time'
  ,range: true
});

laydate.render({
  elem: '#sleep_time'
  ,type: 'time'
  ,range: true
});
laydate.render({
  elem: '#time_remind'
  ,type: 'time'
  ,range: false
});
// 设备参数设置功能js部分结束


 var navigationControl = new BMap.NavigationControl({
    // 靠左上角位置
    anchor: BMAP_ANCHOR_TOP_LEFT,
    // LARGE类型
    type: BMAP_NAVIGATION_CONTROL_LARGE,
    // 启用显示定位
    enableGeolocation: true
  });
  map.addControl(navigationControl);
  // 添加定位控件

var geolocationControl = new BMap.GeolocationControl();
  geolocationControl.addEventListener("locationSuccess", function(e){
    // 定位成功事件
    var address = '';
    address += e.addressComponent.province;
    address += e.addressComponent.city;
    address += e.addressComponent.district;
    address += e.addressComponent.street;
    address += e.addressComponent.streetNumber;
    alert("当前定位地址为：" + address);
  });
  geolocationControl.addEventListener("locationError",function(e){
    // 定位失败事件
    alert(e.message);
  });
  map.addControl(geolocationControl);
//经纬度换算为百度经纬度需要用到的相关函数

var pi = 3.14159265358979324;
var a = 6378245.0;
var ee = 0.00669342162296594323;
var x_pi = 3.14159265358979324 * 3000.0 / 180.0;

function wgs2bd(lat, lon) {
       _wgs2gcj = wgs2gcj(lat, lon);
       _gcj2bd = gcj2bd(_wgs2gcj[0], _wgs2gcj[1]);
       return _gcj2bd;
}

function gcj2bd(lat, lon) {
       x = lon, y = lat;
       z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
       theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
       bd_lon = z * Math.cos(theta) + 0.0065;
       bd_lat = z * Math.sin(theta) + 0.006;
       return [ bd_lat, bd_lon ];
}

function bd2gcj(lat, lon) {
       x = lon - 0.0065, y = lat - 0.006;
       z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
       theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
       gg_lon = z * Math.cos(theta);
       gg_lat = z * Math.sin(theta);
       return [ gg_lat, gg_lon ];
}

function wgs2gcj(lat, lon) {
       dLat = transformLat(lon - 105.0, lat - 35.0);
       dLon = transformLon(lon - 105.0, lat - 35.0);
       radLat = lat / 180.0 * pi;
       magic = Math.sin(radLat);
       magic = 1 - ee * magic * magic;
       sqrtMagic = Math.sqrt(magic);
       dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
       dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
       mgLat = lat + dLat;
       mgLon = lon + dLon;
       return [ mgLat, mgLon ];
}

function transformLat(lat, lon) {
       ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.sqrt(Math.abs(lat));
       ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0;
       ret += (20.0 * Math.sin(lon * pi) + 40.0 * Math.sin(lon / 3.0 * pi)) * 2.0 / 3.0;
       ret += (160.0 * Math.sin(lon / 12.0 * pi) + 320 * Math.sin(lon * pi  / 30.0)) * 2.0 / 3.0;
       return ret;
}

function transformLon(lat, lon) {
       ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.sqrt(Math.abs(lat));
       ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0;
       ret += (20.0 * Math.sin(lat * pi) + 40.0 * Math.sin(lat / 3.0 * pi)) * 2.0 / 3.0;
       ret += (150.0 * Math.sin(lat / 12.0 * pi) + 300.0 * Math.sin(lat / 30.0 * pi)) * 2.0 / 3.0;
       return ret;
}



</script>